package com.jeecg.p3.system.order.web;

import com.jeecg.p3.base.util.ComCode;
import com.jeecg.p3.base.util.DateUtil;
import com.jeecg.p3.base.util.RandomUtil;
import com.jeecg.p3.base.util.WxUserInfo;
import com.jeecg.p3.system.base.BaseController;
import com.jeecg.p3.system.order.dao.CourseOrderDao;
import com.jeecg.p3.system.order.entity.CourseOrder;
import com.jeecg.p3.system.order.entity.ProductDetails;
import com.jeecg.p3.system.order.service.CourseOrderService;
import com.jeecg.p3.system.order.service.ProductDetailsService;
import com.jeecg.p3.system.vo.LoginUser;
import org.apache.velocity.VelocityContext;
import org.jeecgframework.p3.core.common.utils.AjaxJson;
import org.jeecgframework.p3.core.util.SystemTools;
import org.jeecgframework.p3.core.util.plugin.ViewVelocity;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 全部订单处理类
 */
@Controller
@RequestMapping("/system/back/order/all")
public class AllOrderController extends BaseController {

    @Autowired
    private CourseOrderService courseOrderService;

    @Autowired
    private ProductDetailsService productDetailsService;

    @Autowired
    private CourseOrderDao courseOrderDao;

    @RequestMapping(value="/list.do",method = {RequestMethod.GET,RequestMethod.POST})
    public void toIndividual(@ModelAttribute CourseOrder query, HttpServletRequest request, HttpServletResponse response,
                             @RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo,
                             @RequestParam(required = false, value = "pageSize", defaultValue = "10") int pageSize) throws Exception{
        PageQuery<CourseOrder> pageQuery = new PageQuery<CourseOrder>();
        pageQuery.setPageNo(pageNo);
        pageQuery.setPageSize(pageSize);
        VelocityContext velocityContext = new VelocityContext();
        pageQuery.setQuery(query);
        velocityContext.put("CourseOrder",query);
        velocityContext.put("pageInfos", SystemTools.convertPaginatedList(courseOrderService.queryPageList(pageQuery)));
        String viewName = "system/back/order/all_order.vm";
        ViewVelocity.view(request,response,viewName,velocityContext);
    }


    /**
     * 保存订单信息1 以及订单详情信息
     * @return
     */
    @RequestMapping(value = "/doAdd",method ={RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public AjaxJson doAdd(String userName,String money,String productId,String openId,String couponId,
                          HttpServletRequest request){
        AjaxJson j = new AjaxJson();
        ProductDetails productDetails=new ProductDetails();
        try {
            WxUserInfo wxUserInfo = getWxUserInfo(request);
            CourseOrder courseOrder = courseOrderService.doAdd(userName, money, productId, openId, couponId,
                    wxUserInfo.getHeadimgurl());
            CourseOrder order = courseOrderService.findByNo(courseOrder.getDataLsh());
            productDetails.setCouponId(couponId);
            productDetails.setStatus("1");
            productDetails.setProductId(productId);
            productDetails.setOpenId(openId);
            productDetails.setOrderId(order.getId());
            productDetails.setUserStatus(0);//初始状态为0 不可用  1可用
            productDetailsService.doAdd(productDetails);
            j.setMsg("保存订单及详情成功");
            j.setObj(courseOrder);
            j.setSuccess(true);
        } catch (Exception e) {
            log.info(e.getMessage());
            j.setSuccess(false);
            j.setMsg("保存订单及详情失败");
        }
        return j;
    }

    @ResponseBody
    @RequestMapping(value = "/findByopenId",method ={RequestMethod.GET, RequestMethod.POST})
    public AjaxJson findByopenId(String openId,Integer payStatus ){
        AjaxJson j = new AjaxJson();
        CourseOrder param = new CourseOrder();
        if(!openId.isEmpty()){
            param.setOpenId(openId);
            param.setPayStatus(payStatus);
            List<CourseOrder> list = courseOrderService.findByParam(param);
            Map<String, Object> map = new HashMap<>();
            map.put("list", list);
           j.setAttributes(map);
           j.setSuccess(true);
           j.setMsg("查询成功");
        }else {
            j.setSuccess(false);
            j.setMsg("查询失败，openId为空");
        }
        return j;
    }


    /**
     * 修改订单1
     * @param orderNo
     * @return
     */
    @RequestMapping(value = "/updateByopenId",method ={RequestMethod.GET, RequestMethod.POST})
    public AjaxJson updateByopenId(String orderNo ){
        AjaxJson j = new AjaxJson();
        CourseOrder param = courseOrderService.findByNo(orderNo);
        param.setPayStatus(2);
        courseOrderDao.update(param);
            j.setSuccess(true);
            j.setMsg("查询成功");
        return j;
    }


}
